package com.beater.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.beater.controller.bean.ResponseBean;
import com.beater.controller.bean.ResponseBodyJson;
import com.beater.dao.beans.User;
import com.beater.exception.BusinessException;
import com.beater.service.IUserService;
import com.beater.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("user")
public class UserController {
	 @Autowired
	   IUserService userService;  

	    /**
	     *跳转到添加用户界面
	     */
	    @RequestMapping("toAddUser")
	    public ResponseBean toAddUser(){
	    	
	        return null;
	    }

	    /**
	     * 注册
	     * @param model
	     * @param user
	     * @return
	     */
	    @RequestMapping("register") 
	    @ResponseBody
	    public ResponseBodyJson<User> addUser(User user) {
	        try {
				if(user != null){
					User tUser = userService.getUser(user.getUphone()) ;
					if (null != tUser) {
						return ResponseBodyJson.createErrorResBody("该用户已注册") ;
					}
				   if (userService.saveUser(user)>0) {
					   return ResponseBodyJson.createSuccessResBody("注册成功", null) ;
				   }
					
				}
				return ResponseBodyJson.createErrorResBody("注册失败") ;
			} catch (Exception e) {
				e.printStackTrace();
				return ResponseBodyJson.createErrorResBody("注册失败") ;
			}
	    }

	    /**
	     * 修改用户
	     * @param model
	     * @param request
	     * @param user
	     * @return
	     */
	   /* @RequestMapping("updateUser")  
	    public String UpdateUser(Model model,User user){
	             if(userService.updateUser(user)){  
	                 //   user = userService.findUserById(user.getUserId());  
	                    model.addAttribute("user", user);  
	                    return "redirect:/user/userInfo";  
	                } 
	         return "/error";  
	    }*/

	    /**
	     * 查询所有用户
	     * @param request
	     * @param model
	     * @return
	     */
	    @RequestMapping("getAllUser")  
	    public String getAllUser(Model model){
	       /* List<User> user = userService.findAll();
	        model.addAttribute("userList",user);*/
	        return "allUser";
	    }

	    /**
	     * 查询单个用户
	     * @param id
	     * @param request
	     * @param model
	     * @return
	     */
	    @RequestMapping("/getUser")  
	    @ResponseBody
	    public ResponseBean getUser(HttpServletRequest request){
	    	String phone = request.getParameter("phone") ;
	    	if(StringUtil.isNotEmpty(phone)){
	    		User user = userService.getUser(phone) ;
	    		if(null != user){
	    			return ResponseBean.toSuccessJsonBean(user) ;
	    		}
	    	}	    	
	    	return ResponseBean.toFailJsonBean() ;
	    }
	    /**
	     * 根据id删除用户
	     * @param id
	     * @param request
	     * @param response
	     */
	 //   @RequestMapping("/delUser") 
	  /*  public String deleteUser(int id,Model model){
	        model.addAttribute("user", userService.deleteUser(id));  
	        return "redirect:/user/userInfo";
	    }*/

	    /**
	     * 分页查询用户信息
	     * @param pn 默认从第一页开始  请求参数
	     * @param model
	     * @return
	     */
	    @RequestMapping("userInfo")
	    public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
	        //从第一条开始 每页查询五条数据
	      /*  PageHelper.startPage(pn, 5);
	      //  List<User> users = userService.findAll();
	        //将用户信息放入PageInfo对象里
	        PageInfo page = new PageInfo(users,5);
	        model.addAttribute("pageInfo", page);*/
	        return "allUser";
	    }
}
